package com.dream.vo.base;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.persistence.Transient;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class MvcValidtorResult  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7633391568565975882L;
	
	private Map<String,String> fieldErrors = Maps.newHashMap();
	
	@Transient
	private List<String> globalErrors = Lists.newArrayList();
	
	private String globalMessage = "";

	public Map<String, String> getFieldErrors() {
		return fieldErrors;
	}

	public void setFieldErrors(Map<String, String> fieldErrors) {
		this.fieldErrors = fieldErrors;
	}

	public List<String> getGlobalErrors() {
		return globalErrors;
	}

	public void setGlobalErrors(List<String> globalErrors) {
		this.globalErrors = globalErrors;
	}

	public String getGlobalMessage() {
		
		if(CollectionUtils.isNotEmpty(globalErrors)){
			StringBuilder builder = new StringBuilder();
			int i=1;
			for(String item : globalErrors){
				builder.append(String.format("%d: ", i));
				builder.append(item);
				builder.append("<br>");
			}
			globalMessage = builder.toString();
		}
		
		return globalMessage;
	}
	
	public boolean hasErrors(){
		
		return MapUtils.isNotEmpty(fieldErrors) || CollectionUtils.isNotEmpty(globalErrors);
	}
	
}

